package com.enjoyor.soa.traffic.rest.tsms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.tsms.RltRouteDeviceDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RouteInfoDto;
import com.enjoyor.soa.traffic.api.dto.tsms.SchemeInfoDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.rest.tsms.dubbo.ISchemeRehearseConsumeService;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@Auth
@RestController
@RequestMapping({ "/scheme" })
@Api("SchemeRehearseController相关api")
@CrossOrigin
public class SchemeRehearseController {
	

	@Autowired
	ISchemeRehearseConsumeService iSchemeRehearseDubboConsumeService;	

	/**
	 * 方案冲突-list
	 * 
	 * @return
	 */
	@GetMapping(value = { "/getSchemeRehearseList" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiResponse(response = SchemeInfoDto.class, code = 0, message = "")
	@ApiOperation("方案冲突List集合")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "browserId", value = "浏览器id", paramType = "query", required = true, dataType = "string"),
		@ApiImplicitParam(name = "schemeBeginDate", value = "方案开始时间", paramType = "query", required = true, dataType = "string"),
		@ApiImplicitParam(name = "schemeEndDate", value = "方案结束时间", paramType = "query", required = true, dataType = "string") })
	public ResultPojo getSchemeRehearseList(
			@RequestParam(defaultValue = "", value="browserId", required=true) String browserId,
			@RequestParam(defaultValue = "", value="schemeBeginDate", required=true) String schemeBeginDate,
			@RequestParam(defaultValue = "", value="schemeEndDate", required=true) String schemeEndDate, 
			HttpServletRequest request) {
		boolean isDetail = false;
		return iSchemeRehearseDubboConsumeService.getSchemeRehearseList(browserId, schemeBeginDate,schemeEndDate,isDetail);
	}

	/**
	 * 方案详细冲突
	 * 
	 * @return
	 */
	@GetMapping(value = { "/getSchemeRehearseDetail" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("方案详细冲突")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "browserId", value = "浏览器id", paramType = "query", required = true, dataType = "string"),
		@ApiImplicitParam(name = "executeDate", value = "执行日期", paramType = "query", required = true, dataType = "string") })
	public ResultPojo getSchemeRehearseDetail(
			@RequestParam(defaultValue = "", value="browserId", required=true) String browserId,
			@RequestParam(defaultValue = "", value="executeDate", required=true) String executeDate,
			HttpServletRequest request) {
		boolean isDetail = true;
		return iSchemeRehearseDubboConsumeService.getSchemeRehearseList(browserId, executeDate,executeDate,isDetail);
	}
	
	/**
	 * 预演，推送服务
	 * 
	 * @return
	 */
	@PostMapping(value = { "/getPushServer" }, consumes = { "application/json; charset=UTF-8" }, produces = {
		"application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("预演")
	@ApiImplicitParams({ @ApiImplicitParam(name = "rout", value = "进度百分比", paramType = "query", dataType = "double"),
		@ApiImplicitParam(name = "date", value = "日期", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "times", value = "倍率", paramType = "query", dataType = "double"),
		@ApiImplicitParam(name = "browserId", value = "浏览器id", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "state", value = "状态(start/stop/suspend)", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "isrout", value = "是否只改变进度", paramType = "query", dataType = "Boolean"),
		@ApiImplicitParam(name = "routeInfoList", value = "修改的线路信息", paramType = "body", dataType = "List<RouteInfoDto>")})
	public ResultPojo getPushServer(
			@RequestParam(defaultValue = "0") double rout,
			@RequestParam(defaultValue = "1950-12-12") String date, 
			@RequestParam(defaultValue = "1")double times,
			@RequestParam(defaultValue = "1950-12-12")String browserId,
			@RequestParam(defaultValue = "start")String state,
			@RequestParam(defaultValue = "false")Boolean isrout,
			@RequestBody List<RouteInfoDto> routeInfoList,
			HttpServletRequest request) {
		iSchemeRehearseDubboConsumeService.getPushServer(rout,date, times, state,browserId,isrout,routeInfoList);
		return ResultHelper.getResult("开始预演");
	}
	
	
	/**
	 * 获取推送服务地址
	 */
	@ResponseBody
	@GetMapping(value = "/GetPushServerUrl", produces = "application/json; charset=utf-8")
	@ApiOperation("获取推送服务地址")
	public Map<String, Object> GetPushServerUrl(HttpServletRequest request, HttpServletResponse response) {
		ResultPojo rp = iSchemeRehearseDubboConsumeService.getPushServerUrl();
		//ResultPojo rp = DubboServerPushServiceImpl.getInstance().getService().getBasePath();
		Map<String, Object> r = new HashMap<>();
		r.put("success", true);
		r.put("url", rp.getResultList().toString());
		return r;
	}

}
